<?php

namespace app\admin\controller\user;

use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use think\App;
use app\admin\model\user\User as UserModel;
/**
 * @ControllerAnnotation(title="user")
 */
class User extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\user\User();
        
        $this->assign('select_list_gender', $this->model::SELECT_LIST_GENDER, true);

        $this->assign('select_list_status', $this->model::SELECT_LIST_STATUS, true);

        $this->assign('select_list_is_del', $this->model::SELECT_LIST_IS_DEL, true);

        $this->assign('select_list_source', $this->model::SELECT_LIST_SOURCE, true);

    }

    
    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $UserModel = new UserModel();
                $post['is_certify'] = 1;
                $post['user_type'] = 2;
                $lat = getLat($post['address']);
                $post['lat'] = $lat['lat'];
                $post['lng'] = $lat['lng'];
                $save = $UserModel->get_reg_data($post);
                
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                if($post['password']){
                    $post['password'] = password($post['password']);
                }
                $lat = getLat($post['address']);
                $post['lat'] = $lat['lat'];
                $post['lng'] = $lat['lng'];
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
            //删除用户资产
            \think\facade\Db::name('user_assets')->where('user_id',$id)->delete();
            //删除用户订单
            \think\facade\Db::name('order')->where('user_id',$id)->delete();
            //删除用户流水记录
            \think\facade\Db::name('user_money_log')->where('user_id',$id)->delete();
        } catch (\Exception $e) {
            $this->error('删除失败:' . $e->getMessage());
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }
    
    
}